package chances.epg.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import chances.epg.navigator.EpgExceptionHandleResolver;
import chances.epg.navigator.history.NavigateHistoryInteceptor;
import chances.epg.navigator.interceptor.AccessLogInterceptor;
import chances.epg.session.UserSessionInterceptor;

@Configuration
@PropertySource("/WEB-INF/conf/epg.properties")
@Qualifier("epgConfig")
@ComponentScan(basePackages = "chances.epg.navigator.controller"
		+ ",chances.epg.play.ctrl,chances.epg.session.ctrl"
		+",chances.epg.navigator.history"
		+",chances.epg.navigator.service"
		+ ",chances.epg.session,chances.wechat.config")


public class EpgMvcConfig extends BaseMvcConfig {


	@Value("${epg.url.suffix:.epg}")
	private String suffix;

	@Value("${epg.server.url:http://localhost:8080/epg}")
	private String epgHost;
	
	public UserSessionInterceptor userSessionInterceptor() {
		return new UserSessionInterceptor();
	}
	
	
	public AccessLogInterceptor accessLogInterceptor() {
		return new AccessLogInterceptor();
	}



	@Bean
	EpgExceptionHandleResolver exceptionResolver() {
		return new EpgExceptionHandleResolver();
	}

	

	@Override
	public RequestMappingHandlerMapping requestMappingHandlerMapping() {
		return super.requestMappingHandlerMapping();
	}


	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new UserSessionInterceptor()).addPathPatterns("/**/*.epg");
		registry.addInterceptor(accessLogInterceptor());
		registry.addInterceptor(super.getApplicationContext().getBean("navigateHistoryInteceptor",NavigateHistoryInteceptor.class));
	}


	public String getSuffix() {
		return suffix;
	}


	public String getEpgHost() {
		return epgHost;
	}


	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}


	public void setEpgHost(String epgHost) {
		this.epgHost = epgHost;
	}


}
